package com.cherrish.demo.framework;

import com.cherrish.demo.provider.api.HelloService;
import com.cherrish.demo.register.Register;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author cherrish
 * @time 2019-03-20 16:37
 * @name ProxyFactory
 * @desc:
 */
public class ProxyFactory<T> {
    public static <T> T getProxy(Class interfaceClass){
        return (T) Proxy.newProxyInstance(interfaceClass.getClassLoader(), new Class[]{interfaceClass}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                Invocation invocation = new Invocation(HelloService.class.getName(), "sayHello", new Object[]{"MzLiu"}, new Class[]{String.class});
                URL ul = Register.random(interfaceClass.getName());

                /*HttpClient httpClient = new HttpClient();
                return httpClient.post(ul.getHostname(), ul.getPort(), invocation);*/
                //协议切换
                Protocol protocol = ProtocolFactory.getProtocol();
                return protocol.send(ul, invocation);
            }
        });
    }
}
